package cn.fan.whale.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.fan.whale.entity.dto.BaseReturnDto;
import cn.fan.whale.entity.po.SourceEntity;
import cn.fan.whale.service.SourceService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/source")
@CrossOrigin
@SaCheckLogin
public class SourceController {

    private final SourceService sourceService;

    private final StpInterface stpInterface;

    public SourceController(SourceService sourceService, StpInterface stpInterface) {
        this.sourceService = sourceService;
        this.stpInterface = stpInterface;
    }

    @GetMapping("/find")
    public BaseReturnDto find(SourceEntity sourceEntity) {
        List<SourceEntity> list = sourceService.find(sourceEntity);
        return BaseReturnDto.success(list);
    }

    @SaCheckPermission("system.source:save")
    @PostMapping("/save")
    public BaseReturnDto save(@RequestBody SourceEntity sourceEntity) {
        sourceService.save(sourceEntity);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @SaCheckPermission("system.source:update")
    @PostMapping("/update")
    public BaseReturnDto update(@RequestBody SourceEntity sourceEntity) {
        sourceService.update(sourceEntity);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @SaCheckPermission("system.source:delete")
    @GetMapping("/delete")
    public BaseReturnDto delete(Integer sourceId) {
        sourceService.delete(sourceId);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @GetMapping("/getPermissionList")
    public BaseReturnDto getSourceByUserId() {
        Integer userId = StpUtil.getLoginIdAsInt();
        List<String> list = stpInterface.getPermissionList(userId, null);
        return BaseReturnDto.success(list);
    }

    @GetMapping("/getSourceIdListByRoleId")
    public BaseReturnDto getSourceIdListByRoleId(Integer roleId) {
        List<Integer> list = sourceService.getSourceIdListByRoleId(roleId);
        return BaseReturnDto.success(list);
    }

    @GetMapping("/getSourceList")
    public BaseReturnDto getSourceList() {
        Integer userId = StpUtil.getLoginIdAsInt();
        List<SourceEntity> list = sourceService.getSourceListByUserId(userId);
        return BaseReturnDto.success(list);
    }
}
